package model

import (
	"crypto/md5"
	"encoding/hex"
)

const (
	MinUserNameLen = 1
	MinPasswordLen = 1
	NormalCustomer = "customer"
	NormalSeller   = "seller"
)

type LoginUser struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

// 注册时需要定义类型
type RegisterUser struct {
	LoginUser
	// 包含商家  用户
	Kind string `json:"kind"`
}

// 完整的用户信息
type User struct {
	Id int `gorm:"primary_key;auto_increment"`
	// 设置唯一索引，关键字有点记不清了
	// Username string  `gorm:"type:varchar(20);uniqueIndex"`
	Username string `gorm:"type:varchar(20)"`
	Kind     string `gorm:"type:varchar(20)"`
	Password string `gorm:"type:varchar(32)"`
}

func (user User) IsCustomer() bool {
	return user.Kind == NormalCustomer
}

func (user User) IsSeller() bool {
	return user.Kind == NormalSeller
}

func IsValidKind(kind string) bool {
	return kind == NormalCustomer || kind == NormalSeller
}

// 对密码进行加密
func GetMD5(text string) string {
	hash := md5.Sum([]byte(text))
	return hex.EncodeToString(hash[:])
}
